package com.yihu.wlyy.repository;

import java.util.Date;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.yihu.wlyy.entity.consult.ConsultMoVi;
import com.yihu.wlyy.entity.doctor.DoctorComment;

public interface ConsultMoViDao extends PagingAndSortingRepository<ConsultMoVi, Long>, JpaSpecificationExecutor<ConsultMoVi> {

	// 根據consult查詢咨询记录
	ConsultMoVi findByConsult(String consult);

	// 查询医生预约列表
	@Query("select a from ConsultMoVi a, ConsultMoViReferral b where a.consult = b.consult and b.to = ?1 and a.type = ?2 and b.talkTime = 0 and a.del = '1'")
	Page<ConsultMoVi> findByDoctor(String doctor, int type, Pageable pageRequest);
	
	@Query("select a from ConsultMoVi a, ConsultMoViReferral b where a.consult = b.consult and b.to = ?1 and a.type = ?2 and a.appoDate < ?3 and b.talkTime = 0 and a.del = '1'")
	Page<ConsultMoVi> findByDoctor(String doctor, int type, Date appoDate, Pageable pageRequest);

	// 查询医生处理过记录列表
	@Query("select a from ConsultMoVi a, ConsultMoViReferral b where a.consult = b.consult and b.to = ?1 and a.type = ?2 and b.talkTime > 0 and a.del = '1'")
	Page<ConsultMoVi> findDoneByDoctor(String doctor, int type, Pageable pageRequest);
	
	@Query("select a from ConsultMoVi a, ConsultMoViReferral b where a.consult = b.consult and b.to = ?1 and a.type = ?2 and a.appoDate < ?3 and b.talkTime > 0 and a.del = '1'")
	Page<ConsultMoVi> findDoneByDoctor(String doctor, int type, Date appoDate, Pageable pageRequest);

	// 查询患者预约列表
	@Query("select a from ConsultMoVi a where a.patient = ?1 and a.type = ?2 and a.status = 0 and del = '1'")
	Page<ConsultMoVi> findByPatient(String patient, int type, Pageable pageRequest);

	// 查询患者已完成记录列表
	@Query("select a from ConsultMoVi a where a.patient = ?1 and a.type = ?2 and a.status <> 0 and del = '1'")
	Page<ConsultMoVi> findDoneByPatient(String patient, int type, Pageable pageRequest);

	// 按患者查询列表
	@Query("select a from ConsultMoVi a where a.patient = ?1 and del = '1'")
	Page<ConsultMoVi> findByPatient(String patient, Pageable pageRequest);

	// 按患者查询列表
	@Query("select a from ConsultMoVi a where a.patient = ?1 and a.appoDate < ?2 and del = '1'")
	Page<ConsultMoVi> findByPatient(String patient, Date appoDate, Pageable pageRequest);

	// 更新患者评价
	@Modifying
	@Query("update ConsultMoVi a set a.comment = ?1 where a.consult = ?2")
	int updateComment(DoctorComment comment, String consult);

	// 更新咨询状态
	@Modifying
	@Query("update ConsultMoVi a set a.status = ?1 where a.consult = ?2")
	int updateStatus(int status, String consult);
}
